

package GUI;

/**
 *
 * @author David Ponce
 * @author Philipp Armbruster
 */

import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class AirlineEdit extends JPanel {

    final Main td;
    private JPanel jp;
    public JLabel label2;
    public JTextField textfield1;
    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Konstruktor
    public AirlineEdit(final Main td) {

        this.td = td;
        showAirlineEdit();
    }

    
    // Methoden
    public void showAirlineEdit(){

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************
        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 3;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Fluglinie bearbeiten</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        label2 = new JLabel();

        JLabel label3 = new JLabel("Name: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        jp.add(label3, constraints);

        textfield1 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        jp.add(textfield1, constraints);


        //Buttons
        //**********************************************************************

        JButton button_UpdateAirline = new JButton("Änderungen speichern");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        jp.add(button_UpdateAirline, constraints);

        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        jp.add(button_Cancel, constraints);

        
        //Action Listener
        //**********************************************************************

        button_UpdateAirline.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                if ((textfield1.getText().equals("")))
                {
                    JOptionPane.showMessageDialog(null, "Bitte Kennzeichen angeben!");
                }
                else {
                    try {

                        String id = label2.getText();
                        int idAsInt = Integer.parseInt(id);

                        admin.updateAirline(idAsInt, textfield1.getText());

                        td.updateAirline();

                        JOptionPane.showMessageDialog(null, "Fluglinie geändert");
                        ((CardLayout) td.rightp.getLayout()).show(td.rightp,"AirlineM");

                    } catch (RemoteException ex) {
                        Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });


        button_Cancel.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){

                ((CardLayout) td.rightp.getLayout()).show(td.rightp,"AirlineM");
            }
        });

    }

    public JPanel getJp() {
        return jp;
    }
}